using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Practices.EnterpriseLibrary.Validation;

namespace CustomerSample
{
	/// <summary>
	/// Interaction logic for Window1.xaml
	/// </summary>

	public partial class Window1 : System.Windows.Window
	{
		public Window1()
		{
			InitializeComponent();

			this.DataContext = new Customer();
		}

		private void OnValidate(object sender, EventArgs e)
		{
			ValidationResults results = ((Customer)this.DataContext).Validate();

			if (results.IsValid)
			{
				MessageBox.Show("Valid!");
			}
			else
			{
				MessageBox.Show("Errors: \n" + FormatMessages(results));
			}
		}

		private string FormatMessages(ValidationResults results)
		{
			StringBuilder builder = new StringBuilder();
			FormatMessages(results, builder, "");

			return builder.ToString();
		}

		private void FormatMessages(IEnumerable<Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult> results, StringBuilder builder, string indent)
		{
			foreach (Microsoft.Practices.EnterpriseLibrary.Validation.ValidationResult result in results)
			{
				builder.AppendLine(indent + result.Message);
				FormatMessages(result.NestedValidationResults, builder, indent + "  ");
			}
		}
	}
}